package bTree.LeetCode;

public class 求根节点到叶节点数字之和 {
    int sum=0;
    public int sumNumbers(TreeNode root) {
        StringBuilder sb=new StringBuilder();
        dfs(root,sb);
        return sum;
    }

    private void dfs(TreeNode node, StringBuilder sb) {
        if(node.left==null&&node.right==null){
            sb.append(node.val);
            int i = Integer.parseInt(sb.toString());
            sum+=i;
            sb.deleteCharAt(sb.length()-1);
            return;
        }
        sb.append(node.val);
        dfs(node.left,new StringBuilder(sb));
        dfs(node.right,new StringBuilder(sb));
    }
}
